//
//  ViewController.m
//  Demo1
//
//  Created by ZhouWeiMing on 14/7/23.
//  Copyright (c) 2014年 zhishi. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
	
	bgView = [[FXBlurView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height)];
	bgView.backgroundColor = [UIColor blueColor];
	[self.view addSubview:bgView];
	[self.view bringSubviewToFront:bgView];
	
	UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tgrClick)];
	bgView.userInteractionEnabled = YES;
	[bgView addGestureRecognizer:tgr];
	
	
	NSArray *arrayText = [NSArray arrayWithObjects:@"word1",@"photo",@"拍照",@"签到",@"点评111",@"more", nil];

	
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j< 3; j++) {
			UIButton *btn = [self createBtnWithX:i andWithY:j];
			[bgView addSubview:btn];
			
			UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20+j*95, 180+i*115+85, 80, 20)];
			label.backgroundColor = [UIColor clearColor];
			label.textAlignment = NSTextAlignmentCenter;
			label.text = [arrayText objectAtIndex:3*i+j];
			[bgView addSubview:label];
		}
	}
}

-(void)tgrClick{
	[UIView animateWithDuration:0.3 animations:^{
		
		for (UIView *btn in bgView.subviews) {
			if ([btn isKindOfClass:[UIButton class]]) {
				if (btn.tag == 0 || btn.tag == 3) {
					btn.frame = CGRectMake(20, self.view.bounds.size.height+10, 80, 80);
				}
				if (btn.tag == 1 || btn.tag == 4) {
					btn.frame = CGRectMake(20+95, self.view.bounds.size.height+10, 80, 80);
				}
				if (btn.tag == 2 || btn.tag == 5) {
					btn.frame = CGRectMake(20+95*2, self.view.bounds.size.height+10, 80, 80);
				}
				btn.alpha = 0.0;

			}
		}
		
	}completion:^(BOOL finished) {
		
		[UIView animateWithDuration:0.2 animations:^{
			bgView.alpha = 0.0;
		}];
	}];
}

-(UIButton *)createBtnWithX:(int)x andWithY:(int)y{
	UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
	btn.backgroundColor = [UIColor redColor];
	btn.frame = CGRectMake(20+y*95, 180+x*115, 80, 80);
	btn.tag = 3 * x + y;
	btn.layer.masksToBounds = YES;
	btn.layer.cornerRadius = 40;
	[btn addTarget:self action:@selector(btnClick1:) forControlEvents:UIControlEventTouchUpInside];
	return btn;
}



-(void)btnClick1:(UIButton *)btn{
	NSLog(@"btn.tag = %d",btn.tag);

}
-(void)btnClick:(id)sender{
	[UIView animateWithDuration:0.2 animations:^{
		bgView.alpha = 1.0;
	}completion:^(BOOL finished) {
		[UIView animateWithDuration:0.2 animations:^{
			for (UIView *btn in bgView.subviews) {
				if ([btn isKindOfClass:[UIButton class]]) {
					if (btn.tag == 0) {
						btn.frame = CGRectMake(20, 180, 80, 80);
					}
					if (btn.tag == 1) {
						btn.frame = CGRectMake(20+95, 180, 80, 80);
					}
					if (btn.tag == 2) {
						btn.frame = CGRectMake(20+95*2, 180, 80, 80);
					}
					btn.alpha = 1.0;
				}
			}
		}completion:^(BOOL finished) {
			[UIView animateWithDuration:0.1 animations:^{
				for (UIView *btn in bgView.subviews) {
					if ([btn isKindOfClass:[UIButton class]]) {
						if (btn.tag == 3) {
							btn.frame = CGRectMake(20, 180+115, 80, 80);
						}
						if (btn.tag == 4) {
							btn.frame = CGRectMake(20+95, 180+115, 80, 80);
						}
						if (btn.tag == 5) {
							btn.frame = CGRectMake(20+95*2, 180+115, 80, 80);
						}
						btn.alpha = 1.0;
					}
					
					CGFloat t = 4.0;
					CGAffineTransform translateRight  =CGAffineTransformTranslate(CGAffineTransformIdentity, 0.0,t);
					CGAffineTransform translateLeft =CGAffineTransformTranslate(CGAffineTransformIdentity,0.0,-t);
					
					btn.transform = translateLeft;
					
					[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{
						[UIView setAnimationRepeatCount:1.0];
						btn.transform = translateRight;
					} completion:^(BOOL finished){
						if(finished){
							[UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
								btn.transform =CGAffineTransformIdentity;
							} completion:NULL];
						}
					}];
				}
				
				
			}];
		}];

	}];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
